package de.gwtprime.client.views;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.Widget;

import de.gwtprime.client.event.UserSettingsChangedEvent;
import de.gwtprime.client.event.handler.UserSettingsChangedEventHandler;
import de.gwtprime.client.user.ACLManager;
import de.gwtprime.client.user.ACLManager.PrimeUIElement;
import de.gwtprime.client.util.AppUtils;
import de.gwtprime.client.views.popup.ChangeUserSettingsPopup;
import de.gwtprime.client.widgets.QuestionPreviewWidget;
import de.gwtprime.shared.requestFactory.QuestionProxy;
import de.gwtprime.shared.requestFactory.User2Proxy;

public class UserAccountViewImpl extends Composite implements UserAccountView{

	public static ServiceViewImplUiBinder uiBinder = GWT.create(ServiceViewImplUiBinder.class);
	interface ServiceViewImplUiBinder extends UiBinder<Widget, UserAccountViewImpl> {}

	private String name;
	
	private Presenter presenter;


	PopupPanel popup = new PopupPanel(true);
	@UiField Label emailVal;
	
	@UiField Label userNameLbl;
	@UiField Label userNameVal;
	
	@UiField Label karmaVal;
	@UiField Label karmaLbl;
	
	
	@UiField FlowPanel questionPanel;
	
	@UiField Button changeAcctounBtn;

	public UserAccountViewImpl(){
		initWidget(uiBinder.createAndBindUi(this));

		
		userNameLbl.setText("Name: ");
		karmaLbl.setText("Karma: ");
		
		
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
		
		presenter.getClientFactory().getEventBus().addHandler(UserSettingsChangedEvent.TYPE, new UserSettingsChangedEventHandler() {
			
			@Override
			public void onUserSettingsChanged(UserSettingsChangedEvent event) {
				System.out.println("aaa");
				userNameVal.setText(event.getUser().getName());
				popup.hide();
			}
		});
		
	}

	public Presenter getPresenter(){
		return presenter;
	}

	@Override
	public void setName(String name) {
		this.name = name;
	}
	
	public String getName(){
		return name;
	}

	

	@Override
	public void setUser(final User2Proxy user) {

		if(user==null) return;
		
		emailVal.setText(user.getEmail());
		karmaVal.setText(user.getKarma() == null ? "0" : String.valueOf(user.getKarma()));
		
		userNameVal.setText(user.getName());
		
		changeAcctounBtn.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				
				
				popup.clear();
				popup.setWidget(new ChangeUserSettingsPopup(presenter.getClientFactory()));
				popup.setWidth(AppUtils.popupWidth);
				
				popup.setGlassEnabled(true);
				popup.center();
				
			}
		});
		
		changeAcctounBtn.setVisible(ACLManager.hasUserAccess(user, PrimeUIElement.EditProfile));
	}

	@Override
	public void setQuestions(List<QuestionProxy> questions) {
		questionPanel.clear();
		
		for(QuestionProxy question : questions){
			
			questionPanel.add(new QuestionPreviewWidget(question, presenter.getClientFactory(), false));
		}
		
	}



}
